package jsyf.oa.web.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.RoleNameFacade;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Lenovo on 2017/12/7.
 */
@Api(value = "岗位名称接口", description = "岗位名称接口")
@Controller
@RequestMapping("/roleName")
public class RoleNameController {
    @Resource
    private RoleNameFacade roleNameFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String result = roleNameFacade.save(jsonData);
        if(!StringUtils.isEmpty(result)){
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id",result);
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
        }else{
            ResponseUtils.putJsonResponse(response,BaseUtils.errorrRespond("添加岗位失败！"));
        }
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        boolean result = roleNameFacade.delById(jsonParam.getString("id"));
        if(result){
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond());
        }else{
            ResponseUtils.putJsonResponse(response,BaseUtils.errorrRespond("删除岗位失败！"));
        }
    }

    @RequestMapping(value = "/getTreeNodes.json", method = RequestMethod.POST)
    public void getTreeNodes(HttpServletResponse response, @RequestBody String jsonData) {
        List<JSONObject> result = roleNameFacade.getTreeNodes();
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/sort.json", method = RequestMethod.POST)
    public void sort(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean result = roleNameFacade.sort(jsonParam.getString("roleNameIds"));
        JSONObject objResult = new JSONObject();
        objResult.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }
}
